home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / daemons / ipServer / sockMisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-26  |  9.0 KB  |  400 lines

  1. /* 
  2.  * sockMisc.c --
  3.  *
  4.  *    Miscellaneous routines to test and socket state information.
  5.  *
  6.  * Copyright 1987 Regents of the University of California
  7.  * All rights reserved.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/daemons/ipServer/RCS/sockMisc.c,v 1.3 89/03/23 09:56:10 brent Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21.  
  22. #include "sprite.h"
  23. #include "dev/net.h"
  24. #include "ipServer.h"
  25. #include "socket.h"
  26. #include "sockInt.h"
  27.  
  28.  
  29.  
  30. /*
  31.  *----------------------------------------------------------------------
  32.  *
  33.  * Sock_SetError --
  34.  *
  35.  *    Saves the error status in the socket and notifies the client
  36.  *    so it can find out about the error. This routine is used when
  37.  *    the exact socket is known, unlike Sock_ReturnError.
  38.  *
  39.  * Results:
  40.  *    None.
  41.  *
  42.  * Side effects:
  43.  *    The socket state is updated. The client is woken up.
  44.  *
  45.  *----------------------------------------------------------------------
  46.  */
  47.  
  48. void
  49. Sock_SetError(sockPtr, status)
  50.     Sock_SharedInfo    *sockPtr;    /* Sock to be notified. */
  51.     ReturnStatus    status;        /* Error to be saved. */
  52. {
  53.     sockPtr->error = status;
  54.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE|FS_EXCEPTION);
  55. }
  56.  
  57.  
  58. /*
  59.  *----------------------------------------------------------------------
  60.  *
  61.  * Sock_IsOptionSet --
  62.  *
  63.  *    Tests the state of the option for a socket.
  64.  *
  65.  * Results:
  66.  *    TRUE        - the option is set.
  67.  *    FALSE        - the option is not set.
  68.  *
  69.  * Side effects:
  70.  *    None.
  71.  *
  72.  *----------------------------------------------------------------------
  73.  */
  74.  
  75. Boolean
  76. Sock_IsOptionSet(sockPtr, option)
  77.     Sock_SharedInfo    *sockPtr;    /* Sock of interest. */
  78.     int        option;
  79. {
  80.     return(sockPtr->options & option);
  81. }
  82.  
  83.  
  84. /*
  85.  *----------------------------------------------------------------------
  86.  *
  87.  * Sock_HasUsers --
  88.  *
  89.  *    Checks if the socket is being used by at least 1 client.
  90.  *
  91.  * Results:
  92.  *    TRUE    - The socket has clients.
  93.  *    FALSE    - No clients are using the socket.
  94.  *
  95.  * Side effects:
  96.  *    None.
  97.  *
  98.  *----------------------------------------------------------------------
  99.  */
  100.  
  101. Boolean
  102. Sock_HasUsers(sockPtr)
  103.     Sock_SharedInfo *        sockPtr;    /* Sock of interest. */
  104. {
  105.     return(sockPtr->clientCount > 0);
  106. }
  107.  
  108.  
  109. /*
  110.  *----------------------------------------------------------------------
  111.  *
  112.  * Sock_Connected --
  113.  *
  114.  *    Changes the socket state to CONNECTED and notifies
  115.  *    the client that this change has occurred.  Called from 
  116.  *    protocol-related routines.
  117.  *
  118.  * Results:
  119.  *    None.
  120.  *
  121.  * Side effects:
  122.  *    The client is woken up.
  123.  *
  124.  *----------------------------------------------------------------------
  125.  */
  126.  
  127. void
  128. Sock_Connected(sockPtr)
  129.     Sock_SharedInfo *        sockPtr;    /* Sock of interest. */
  130. {
  131.     sockPtr->state = CONNECTED;
  132.     if (sockPtr->parentPtr != (Sock_SharedInfo *) NULL) {
  133.     /*
  134.      * SockPtr is a newly-created connection but is not fully
  135.      * initialized. Notify the parent socket so it can complete
  136.      * the initialization.
  137.      */
  138.     Sock_NotifyWaiter(sockPtr->parentPtr, FS_READABLE|FS_WRITABLE);
  139.  
  140.     /*
  141.      * Set the justEstablished flag after Sock_NofityWaiter because that
  142.      * routine indirectly resets it.
  143.      */
  144.     sockPtr->parentPtr->justEstablished = TRUE;
  145.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE);
  146.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE);
  147.     } else {
  148.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE);
  149.     sockPtr->justEstablished = TRUE;
  150.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE);
  151.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE);
  152.     }
  153. }
  154.  
  155.  
  156. /*
  157.  *----------------------------------------------------------------------
  158.  *
  159.  * Sock_Disconnected --
  160.  *
  161.  *    Changes the socket state to DISCONNECTED and sets flags
  162.  *    so that further I/O on the socket is prevented. The client
  163.  *    is notified of the change. Called from protocol-related routines
  164.  *    when the connection is completely closed.
  165.  *
  166.  * Results:
  167.  *    None.
  168.  *
  169.  * Side effects:
  170.  *    The socket state is updated. The client is woken up.
  171.  *
  172.  *----------------------------------------------------------------------
  173.  */
  174.  
  175. void
  176. Sock_Disconnected(sockPtr)
  177.     Sock_SharedInfo *        sockPtr;    /* Sock_ of interest. */
  178. {
  179.     sockPtr->state = DISCONNECTED;
  180.     sockPtr->flags |= (SOCK_STOP_RECV|SOCK_STOP_SEND);
  181.     Sock_NotifyWaiter(sockPtr, FS_READABLE|FS_WRITABLE);
  182. }
  183.  
  184.  
  185. /*
  186.  *----------------------------------------------------------------------
  187.  *
  188.  * Sock_Disconnecting --
  189.  *
  190.  *    Changes the socket state to DISCONNECTING and sets flags
  191.  *    so that further I/O on the socket is prevented. Called from 
  192.  *    protocol-related routines that need to close down a connection 
  193.  *    in several steps.
  194.  *
  195.  * Results:
  196.  *    None.
  197.  *
  198.  * Side effects:
  199.  *    The socket state is updated.
  200.  *
  201.  *----------------------------------------------------------------------
  202.  */
  203.  
  204. void
  205. Sock_Disconnecting(sockPtr)
  206.     Sock_SharedInfo *        sockPtr;    /* Sock of interest. */
  207. {
  208.     sockPtr->state = DISCONNECTING;
  209.     sockPtr->flags |= (SOCK_STOP_RECV|SOCK_STOP_SEND);
  210. }
  211.  
  212. /*
  213.  *----------------------------------------------------------------------
  214.  *
  215.  * Sock_IsRecvStopped --
  216.  *
  217.  *    Returns the state of the STOP_RECV flag.
  218.  *
  219.  * Results:
  220.  *    TRUE    - no more data will be received from the network.
  221.  *    FALSE    - data can be received.
  222.  *
  223.  * Side effects:
  224.  *    None.
  225.  *
  226.  *----------------------------------------------------------------------
  227.  */
  228.  
  229. Boolean
  230. Sock_IsRecvStopped(sockPtr)
  231.     Sock_SharedInfo *        sockPtr;    /* Sock of interest. */
  232. {
  233.     return(sockPtr->flags & SOCK_STOP_RECV);
  234. }
  235.  
  236. /*
  237.  *----------------------------------------------------------------------
  238.  *
  239.  * Sock_IsSendStopped --
  240.  *
  241.  *    Returns the state of the STOP_SEND flag.
  242.  *
  243.  * Results:
  244.  *    TRUE    - no more data can be sent.
  245.  *    FALSE    - data can be sent.
  246.  *
  247.  * Side effects:
  248.  *    None.
  249.  *
  250.  *----------------------------------------------------------------------
  251.  */
  252.  
  253. Boolean
  254. Sock_IsSendStopped(sockPtr)
  255.     Sock_SharedInfo *        sockPtr;    /* Sock of interest. */
  256. {
  257.     return(sockPtr->flags & SOCK_STOP_SEND);
  258. }
  259.  
  260. /*
  261.  *----------------------------------------------------------------------
  262.  *
  263.  * Sock_StopRecv --
  264.  *
  265.  *    Sets a flag to indicate that further reception of data from
  266.  *    the network has been stopped.
  267.  *
  268.  * Results:
  269.  *    None.
  270.  *
  271.  * Side effects:
  272.  *    The socket state is updated.
  273.  *
  274.  *----------------------------------------------------------------------
  275.  */
  276.  
  277. void
  278. Sock_StopRecv(sockPtr)
  279.     Sock_SharedInfo *        sockPtr;    /* Sock_ of interest. */
  280. {
  281.     sockPtr->flags |= SOCK_STOP_RECV;
  282. }
  283.  
  284. /*
  285.  *----------------------------------------------------------------------
  286.  *
  287.  * Sock_StopSending --
  288.  *
  289.  *    Sets a flag to indicate that further sending of data to
  290.  *    the network has been stopped.
  291.  *
  292.  * Results:
  293.  *    None.
  294.  *
  295.  * Side effects:
  296.  *    The socket state is updated.
  297.  *
  298.  *----------------------------------------------------------------------
  299.  */
  300.  
  301. void
  302. Sock_StopSending(sockPtr)
  303.     Sock_SharedInfo *        sockPtr;    /* Sock_ of interest. */
  304. {
  305.     sockPtr->flags |= SOCK_STOP_SEND;
  306. }
  307.  
  308. /*
  309.  *----------------------------------------------------------------------
  310.  *
  311.  * Sock_UrgentDataNext --
  312.  *
  313.  *    Sets a flag to indicate that urgent data is logically at the
  314.  *    front of the receive buffer.
  315.  *
  316.  * Results:
  317.  *    None.
  318.  *
  319.  * Side effects:
  320.  *    The socket flags are updated.
  321.  *
  322.  *----------------------------------------------------------------------
  323.  */
  324.  
  325. void
  326. Sock_UrgentDataNext(sockPtr)
  327.     Sock_SharedInfo *        sockPtr;    /* Sock_S of interest. */
  328. {
  329.     sockPtr->flags |= SOCK_URGENT_DATA_NEXT;
  330. }
  331.  
  332.  
  333. /*
  334.  *----------------------------------------------------------------------
  335.  *
  336.  * Sock_HaveUrgentData --
  337.  *
  338.  *    Notifies the client and controlling process or family that
  339.  *    urgent data has arrived.
  340.  *
  341.  * Results:
  342.  *    None.
  343.  *
  344.  * Side effects:
  345.  *    A signal may be sent to the controlling process or family.
  346.  *    Any users waiting for urgent data are woken up.
  347.  *
  348.  *----------------------------------------------------------------------
  349.  */
  350.  
  351. void
  352. Sock_HaveUrgentData(sockPtr)
  353.     Sock_SharedInfo *        sockPtr;    /* Sock_ of interest. */
  354. {
  355.     /*
  356.      * The owner PID is non-zero, then signal the owner process or family
  357.      * that there's urgent data.
  358.      */
  359.     Sock_NotifyWaiter(sockPtr, FS_EXCEPTION);
  360.     if (sockPtr->owner.id != 0) {
  361.     if (ips_Debug) {
  362.         (void) fprintf(stderr, 
  363.         "Sock_HaveUrgentData: sending URGENT signal to %x (%s)\n",
  364.         sockPtr->owner.id, 
  365.         sockPtr->owner.procOrFamily == IOC_OWNER_FAMILY ? 
  366.             "family" : "process");
  367.     }
  368.  
  369.     Sig_Send(SIG_URGENT, sockPtr->owner.id, 
  370.             sockPtr->owner.procOrFamily == IOC_OWNER_FAMILY);
  371.     }
  372. }
  373.  
  374.  
  375. /*
  376.  *----------------------------------------------------------------------
  377.  *
  378.  * Sock_BadRoute --
  379.  *
  380.  *    Called when a protocol-dependent routine has determined
  381.  *    that a route is bad.
  382.  *
  383.  *    Just a stub procedure for now.
  384.  *
  385.  * Results:
  386.  *    None.
  387.  *
  388.  * Side effects:
  389.  *    None.
  390.  *
  391.  *----------------------------------------------------------------------
  392.  */
  393.  
  394. /*ARGSUSED*/
  395. void
  396. Sock_BadRoute(sockPtr)
  397.     Sock_SharedInfo     *sockPtr;    /* Socket with the bad route. */
  398. {
  399. }
  400.